/*
 * @Date: 2023-01-11 12:58:36
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-05 14:06:36
 * @FilePath: \A19\backend\src\utils\upload.ts
 */
import fs from "fs/promises";
import { serverUrl, serverPort } from '../dev.json'
import { ObjectId } from "mongodb";
//path: uploads/...
export default async function ({ folder, typeCate, id, type }: { folder: string, typeCate: string, id?: ObjectId | string, type: string }, fileData: Uint8Array) {
    try {
        const path = `${folder}/${typeCate}${id || ''}${new Date().getTime()}.${type}`//image63b8eed76bd85268791e2baf1673417134777.png
        await fs.writeFile('uploads/' + path, fileData)
        const name = `/${path}`
        return name
    } catch (error) {
        console.log(error);
        throw new Error('Error')
    }
}